package com.tjsinfo.tjsoa.system.bo;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.hibernate.criterion.SimpleExpression;

import com.tjsinfo.tjsoa.Dao.IQbcDao;
import com.tjsinfo.tjsoa.common.FileManage;
import com.tjsinfo.tjsoa.system.dao.ISystemSpringDao;
import com.tjsinfo.tjsoa.system.dao.ITSystemDepartmentDao;
import com.tjsinfo.tjsoa.system.vo.TSystemDepartment;
import com.tjsinfo.tjsoa.system.vo.TSystemUser;

public class SystemDepartmentBo implements IDepartmentBo{
	private ITSystemDepartmentDao departmentDao;
	private IQbcDao qbcDao;
	
	public IQbcDao getQbcDao() {
		return qbcDao;
	}

	public void setQbcDao(IQbcDao qbcDao) {
		this.qbcDao = qbcDao;
	}

	public ITSystemDepartmentDao getDepartmentDao() {
		return departmentDao;
	}

	public void setDepartmentDao(ITSystemDepartmentDao departmentDao) {
		this.departmentDao = departmentDao;
	}

	private ISystemSpringDao systemSpringDao;
	public void setSystemSpringDao(ISystemSpringDao systemSpringDao) {
		this.systemSpringDao = systemSpringDao;
	}
	public Boolean isParentDepartment(String parentid) {
		// TODO Auto-generated method stub
		
		if (selectByParentID(parentid).size() > 0) {
			return true;
		} else {
			return false;
		}
		
	}

	public List<TSystemDepartment> selectByParentID(String parentid) {
		// TODO Auto-generated method stub
		DetachedCriteria criteria = DetachedCriteria.forClass(TSystemDepartment.class);
		criteria.add(Restrictions.eq("parentId", parentid));
		criteria.add(Restrictions.eq("isDel",1));
		criteria.addOrder(Order.asc("orderNo"));
		return  qbcDao.getCriteria(criteria);
		
	}

	public Integer SelectSystemDepartmentTotalLine() {
		// TODO Auto-generated method stub
		DetachedCriteria criteria = DetachedCriteria.forClass(TSystemDepartment.class);
		criteria.add(Restrictions.eq("isDel", 1));
		return qbcDao.getQueryNumber(criteria, "id");
	}

	public List<TSystemDepartment> selectSystemDepartment(Integer first,
			Integer max) {
		// TODO Auto-generated method stub
		DetachedCriteria criteria = DetachedCriteria.forClass(TSystemDepartment.class);
		criteria.add(Restrictions.eq("isDel", 1));
		List<Order> list = new ArrayList<Order>();
		list.add(Order.asc("orderNo"));
		return qbcDao.findPageByCriteria(criteria, list,max, first).getItems();
		 
	}

	public boolean createAPeopleJS(String url,String sql,String nodeNamePeople){
		return getAPeopleJS(url, sql, nodeNamePeople);
	}
	public boolean createAllOrganizeClickJS(String url,String nodeNamePeople){
		boolean isCreate=true;
		DetachedCriteria dcAll = DetachedCriteria.forClass(TSystemDepartment.class);
		dcAll.addOrder(Order.asc("levelNo"));
		dcAll.addOrder(Order.asc("orderNo"));
		dcAll.addOrder(Order.asc("id"));
		List<TSystemDepartment> departListAll=qbcDao.getCriteria(dcAll);
		String treeClick = getTreeForOnclick(departListAll,nodeNamePeople);
		String fileName2="allOrganizeClick.js";
		FileManage manage = new FileManage();
		try {
			manage.write(treeClick,url,fileName2);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();isCreate=false;
		}
		return isCreate;
	}
	public boolean createOrganizeClickJS(String url,String nodeNamePeople){
		boolean isCreate=true;
		DetachedCriteria dcAll = DetachedCriteria.forClass(TSystemDepartment.class);
		SimpleExpression departs = Restrictions.eq("isDel", Integer.valueOf(1));
		dcAll.add(departs);
		dcAll.addOrder(Order.asc("levelNo"));
		dcAll.addOrder(Order.desc("orderNo"));
		dcAll.addOrder(Order.asc("id"));
		List<TSystemDepartment> departListAll=qbcDao.getCriteria(dcAll);
		String treeClick = getTreeForOnclick(departListAll,nodeNamePeople);
		String fileName2="organizeClick.js";
		FileManage manage = new FileManage();
		try {
			manage.write(treeClick,url,fileName2);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();isCreate=false;
		}
		return isCreate;
	}
	public boolean createPeoplesJS(String url,String nodeNamePeople){
		boolean isCreate=true;
		DetachedCriteria dc = DetachedCriteria.forClass(TSystemDepartment.class);
		SimpleExpression de1 = Restrictions.eq("isDel", Integer.valueOf(1));
		dc.add(de1);
		dc.addOrder(Order.asc("levelNo"));
		dc.addOrder(Order.desc("orderNo"));
		dc.addOrder(Order.asc("id"));
		List<TSystemDepartment> departsList=qbcDao.getCriteria(dc);
		
		DetachedCriteria criteria = DetachedCriteria.forClass(TSystemUser.class);
		SimpleExpression user1 = Restrictions.eq("state", Byte.valueOf("1"));
		criteria.add(user1);
		criteria.addOrder(Order.asc("id"));
		List<TSystemUser> userList=qbcDao.getCriteria(criteria);
		
		return isCreate;
	}
	
	
	
	
	
	
	
	
	
	public boolean getAPeopleJS(String url,String sql,String nodeNamePeople){
		boolean isCreate=true;
		//当前可用部门
		DetachedCriteria dc = DetachedCriteria.forClass(TSystemDepartment.class);
		SimpleExpression de1 = Restrictions.eq("isDel", Integer.valueOf(1));
		dc.add(de1);
		dc.addOrder(Order.asc("levelNo"));
		dc.addOrder(Order.asc("orderNo"));
		dc.addOrder(Order.asc("id"));
		List<TSystemDepartment> departList=qbcDao.getCriteria(dc);
		
		//部门用户列表
		String columns[]={"uid","departId","deptName","userName"};
		List<Map<String, String>> udList=systemSpringDao.commonSelect(columns, sql);
		StringBuffer departIdStringBuffer=new StringBuffer(",");
		StringBuffer peopleStringBuffer=new StringBuffer();
		for (Map<String, String> map : udList) {
			departIdStringBuffer.append(map.get("departId").trim());
			departIdStringBuffer.append(",");
			
			
			
			peopleStringBuffer.append("var nodes"+map.get("uid")+" = new xyTree.NodeNormal('"+map.get("userName")+"');\r\n");
			peopleStringBuffer.append("nodes"+map.get("uid")+".id="+map.get("uid")+"9999;\r\n");
			peopleStringBuffer.append("node"+map.get("departId").trim()+".add(nodes"+map.get("uid")+");\r\n\r\n");
		}
		
		
		String departIdString=departIdStringBuffer.toString();
		StringBuffer sbTree=new StringBuffer();
		String sbIds=",";
		sbTree.append("var treeFenju3 = new xyTree.DivTreeNormal('"+nodeNamePeople+"');\r\n");
		//获取最低级别的长度
		int length=departList.get(0).getId().trim().length();
		String temp="";
		String id="";
		for (TSystemDepartment TSystemDepartment : departList) {
			id=TSystemDepartment.getId().trim();
			if(departIdString.contains(","+id+",")){//部门内是否有人员
				if(id.length()==length){//如果长度在二位的为第一级单位
					sbTree.append("var node"+id+" = new xyTree.NodeNormal('"+TSystemDepartment.getDeptName()+"');\r\n");
					sbTree.append("node"+id+".id="+id+";\r\n");
					sbTree.append("treeFenju3.add(node"+id+");\r\n\r\n");
					sbIds=sbIds+id+",";
				}else{
					temp=id.trim();
					if(sbIds.contains(","+temp.substring(0, temp.length()-2)+",")){
						sbTree.append("var node"+id+" = new xyTree.NodeNormal('"+TSystemDepartment.getDeptName()+"');\r\n");
						sbTree.append("node"+id+".id="+id+";\r\n");
						sbTree.append("node"+temp.substring(0, temp.length()-2)+".add(node"+id+");\r\n\r\n");
						sbIds=sbIds+id+",";
					}
				}
			}
		}
		
		
		sbTree=sbTree.append(peopleStringBuffer);
		FileManage manage = new FileManage();
		try {
			manage.write(sbTree.toString(),url,"aPeopleClick.js");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();isCreate=false;
		}
		return isCreate;
	}
	
	
	
	public String getTreeForOnclick(List<TSystemDepartment> departList,String nodeName) {
		
		
		
		
		
		StringBuffer sbTree=new StringBuffer();
		String sbIds=",";
		sbTree.append("var treeFenju3 = new xyTree.DivTreeNormal('"+nodeName+"');\r\n");
		//获取最低级别的长度		
		int length = 0;
		if (departList.size() > 0) {
			length = departList.get(0).getId().trim().length();
		}
		String temp="";
		String id="";
		for (TSystemDepartment TSystemDepartment : departList) {
			id=TSystemDepartment.getId().trim();
			if(TSystemDepartment.getId().trim().length()==length){//如果长度在二位的为第一级单位
				sbTree.append("var node"+id+" = new xyTree.NodeNormal('"+TSystemDepartment.getDeptName()+"');\r\n");
				sbTree.append("node"+id+".id="+id+";\r\n");
				sbTree.append("treeFenju3.add(node"+id+");\r\n\r\n");
				sbIds=sbIds+id+",";
			}else{
				temp=id.trim();
				if(sbIds.contains(","+temp.substring(0, temp.length()-2)+",")){
					sbTree.append("var node"+id+" = new xyTree.NodeNormal('"+TSystemDepartment.getDeptName()+"');\r\n");
					sbTree.append("node"+id+".id="+id+";\r\n");
					sbTree.append("node"+temp.substring(0, temp.length()-2)+".add(node"+id+");\r\n\r\n");
					sbIds=sbIds+id+",";
				}else{
					
				}
			}
		}
		
		return sbTree.toString();
	}

	public List<TSystemDepartment> getAllDepartment(){
		DetachedCriteria criteria = DetachedCriteria.forClass(TSystemDepartment.class);
		criteria.addOrder(Order.asc("id"));
		List<TSystemDepartment> tjsDepartmentList = qbcDao.getCriteria(criteria);
		
		return tjsDepartmentList;
	}

	public String save(TSystemDepartment department) {
		String message="";
		DetachedCriteria criteria = DetachedCriteria.forClass(TSystemDepartment.class);
		String id=department.getId().toString();
		id=id.substring(0, id.length()-2);
		SimpleExpression depart1 = Restrictions.like("id", id+"%");
		SimpleExpression depart = Restrictions.eq("deptName", department.getDeptName());
		criteria.add(depart1);
		criteria.add(depart);
		List<TSystemDepartment> departmentList=qbcDao.getCriteria(criteria);
		if(departmentList.size()==0){
			if(department.getId().trim().length()>3){//二级以上部门
				department.setParentId(department.getId().trim().substring(0, department.getId().trim().length()-2));
			}else{
				department.setParentId("0");
			}
			departmentDao.save(department);
		}else{
			message="当前部门已存在!";
		}
		return message;
	}
	
	public void update(TSystemDepartment department) {
		qbcDao.update(department);
	}
	
	public void delete(TSystemDepartment persistentInstance){
		departmentDao.delete(persistentInstance);
	}
	
	public TSystemDepartment findById(String id){
		return departmentDao.findById(id);
	}

	public List<TSystemDepartment> selectAllDepartment() {
		// TODO Auto-generated method stub
		DetachedCriteria criteria = DetachedCriteria.forClass(TSystemDepartment.class);
		return  qbcDao.getCriteria(criteria);
	}
}
